cmake_minimum_required(VERSION 3.22)
project(sylar)
#####
include (cmake/utils.cmake)

include_directories(.)
# include_directories(../yaml/yaml-cpp/include/yaml-cpp)

# link_directories(lib)
# link_directories(/usr/lib/x86_64-linux-gnu/)

find_library(PTHREAD pthread)
find_library(OPENSSL_LIBRARY NAMES libssl.a)
find_library(JSONS_CPP NAMES libjson.a /usr/lib /usr/local/lib)
find_library(CRYPTO_LIBRARY NAMES libcrypto.a)

#
#link_directories(../yaml/yaml-cpp/include/yaml-cpp/)

set(LIB_SRC
        sylar/log.cpp
        sylar/util.cpp
        sylar/config.cpp
        sylar/thread.cpp
        sylar/fiber.cpp
        sylar/scheduler.cpp
        sylar/iomanager.cpp
        sylar/timer.cpp sylar/hook.cpp sylar/noncopyable.h
        sylar/fd_manager.cpp sylar/fd_manager.h
        sylar/address.cpp sylar/address.h
        sylar/myendian.h
        sylar/socket.cpp sylar/socket.h
        sylar/bytearray.cpp sylar/bytearray.h
        sylar/http/http.cpp sylar/http/http.h
        sylar/http/http11_parser.cc
        sylar/http/httpclient_parser.cc
        sylar/http/http_parser.cpp
        sylar/tcp_server.cpp
        sylar/stream.cpp
        sylar/stream/socket_stream.cpp
        sylar/http/http_session.cpp
        sylar/http/http_server.cpp
        sylar/http/servlet.cpp
        sylar/http/http_connection.cpp
        sylar/uri.h sylar/uri.cc
        sylar/http/ws_session.h sylar/http/ws_session.cpp
        sylar/http/ws_servlet.cpp sylar/http/ws_servlet.h
        sylar/http/ws_server.cpp sylar/http/ws_server.h
        sylar/hash_util.cpp sylar/hash_util.h
        sylar/chat/chat_servlet.cpp sylar/chat/chat_servlet.h
        sylar/chat/protocol.cpp sylar/chat/protocol.h
        sylar/json_util.cpp sylar/json_util.h
        sylar/chat/resource_servlet.cpp sylar/chat/resource_servlet.h
        sylar/chat/chat_server.cpp sylar/chat/chat_server.h
        sylar/mysql/mysql_connection_pool.cpp
		sylar/mysql/mysql_connection.cpp)

add_library(sylar SHARED ${LIB_SRC})
force_redefine_file_macro_for_sources(sylar)  # 重定义__FILE__为相对路径（之前是绝对路径）
#add_library(sylar_static STATIC ${LIB_SRC})
#SET_TARGET_PROPERTIES (sylar_static PROPERTIES OUTPUT_NAME "sylar")

# 将头文件添加到系统include下
file(GLOB_RECURSE ALL_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
foreach(HEADER ${ALL_HEADERS})
	get_filename_component(HEADER_DIR "${HEADER}" DIRECTORY)
	string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" HEADER_DIR_RELATIVE "${HEADER_DIR}")
	INSTALL(FILES ${HEADER} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${HEADER_DIR_RELATIVE})
endforeach()

# 将动态库文件添加到系统路径下
INSTALL(TARGETS sylar LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/)

set(LIB_LIB
        sylar
        dl
        pthread
        yaml-cpp
		mysqlclient
		${JSONS_CPP}
        ${OPENSSL_LIBRARY}
        ${CRYPTO_LIBRARY})

set(CMAKE_CXX_FLAGS "-Wbuiltin-macro-redefined -Werror -Wno-unused-function -Wno-builtin-macro-redefined")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-Wno-deprecated")

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic")
add_link_options(-rdynamic)
#add_compile_options(-fno-elide-constructors)          # 关闭优化
#set(CMAKE_CXX_FLAGS_DEBUG   "-rdynamic" )             # 调试包不优化
#set(CMAKE_CXX_FLAGS_RELEASE "-Og -DNDEBUG " )   # release包优化
#SET(CMAKE_BUILD_TYPE "Debug")
#SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -g2 -ggdb -rdynamic")
#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3")

add_executable(test tests/test.cpp)
add_dependencies(test sylar)
force_redefine_file_macro_for_sources(test)
target_link_libraries(test sylar yaml-cpp ${LIB_LIB})

add_executable(test_config tests/test_config.cpp)
add_dependencies(test_config sylar)
force_redefine_file_macro_for_sources(test_config)
target_link_libraries(test_config sylar yaml-cpp ${LIB_LIB})

add_executable(test_thread tests/test_thread.cpp)
add_dependencies(test_thread sylar)
force_redefine_file_macro_for_sources(test_thread)
target_link_libraries(test_thread ${LIB_LIB})

add_executable(test_util tests/test_util.cpp)
add_dependencies(test_util sylar)
force_redefine_file_macro_for_sources(test_util)
target_link_libraries(test_util ${LIB_LIB})

add_executable(test_fiber tests/test_fiber.cpp)
add_dependencies(test_fiber sylar)
force_redefine_file_macro_for_sources(test_fiber)
target_link_libraries(test_fiber ${LIB_LIB})

add_executable(test_scheduler tests/test_scheduler.cpp)
add_dependencies(test_scheduler sylar)
force_redefine_file_macro_for_sources(test_scheduler)
target_link_libraries(test_scheduler ${LIB_LIB})

add_executable(test_iomanager tests/test_iomanager.cpp)
add_dependencies(test_iomanager sylar)
force_redefine_file_macro_for_sources(test_iomanager)
target_link_libraries(test_iomanager ${LIB_LIB})

add_executable(test_hook tests/test_hook.cpp)
add_dependencies(test_hook sylar)
force_redefine_file_macro_for_sources(test_hook)
target_link_libraries(test_hook ${LIB_LIB})

add_executable(test_socket tests/test_socket.cpp)
add_dependencies(test_socket sylar)
force_redefine_file_macro_for_sources(test_socket)
target_link_libraries(test_socket ${LIB_LIB})

add_executable(test_bytearray tests/test_bytearray.cpp)
add_dependencies(test_bytearray sylar)
force_redefine_file_macro_for_sources(test_bytearray)
target_link_libraries(test_bytearray ${LIB_LIB})

add_executable(test_http tests/test_http.cpp)
add_dependencies(test_http sylar)
force_redefine_file_macro_for_sources(test_http)
target_link_libraries(test_http ${LIB_LIB})

add_executable(test_http_parser tests/test_http_parser.cpp)
add_dependencies(test_http_parser sylar)
force_redefine_file_macro_for_sources(test_http_parser)
target_link_libraries(test_http_parser ${LIB_LIB})

add_executable(test_tcp_server tests/test_tcp_server.cpp)
add_dependencies(test_tcp_server sylar)
force_redefine_file_macro_for_sources(test_tcp_server)
target_link_libraries(test_tcp_server ${LIB_LIB})

add_executable(echo_server tests/echo_server.cpp)
add_dependencies(echo_server sylar)
force_redefine_file_macro_for_sources(echo_server)
target_link_libraries(echo_server ${LIB_LIB})

add_executable(test_http_server tests/test_http_server.cpp)
add_dependencies(test_http_server sylar)
force_redefine_file_macro_for_sources(test_http_server)
target_link_libraries(test_http_server ${LIB_LIB})

add_executable(test_http_connection tests/test_http_connection.cpp)
add_dependencies(test_http_connection sylar)
force_redefine_file_macro_for_sources(test_http_connection)
target_link_libraries(test_http_connection ${LIB_LIB})

add_executable(test_uri tests/test_uri.cpp)
add_dependencies(test_uri sylar)
force_redefine_file_macro_for_sources(test_uri)
target_link_libraries(test_uri ${LIB_LIB})

add_executable(test_ws_server tests/test_ws_server.cpp)
add_dependencies(test_ws_server sylar)
force_redefine_file_macro_for_sources(test_ws_server)
target_link_libraries(test_ws_server ${LIB_LIB})
target_link_libraries(test_ws_server ${OPENSSL_LIBRARY} ${CRYPTO_LIBRARY})

add_executable(chat_room sylar/chat/chat_main.cpp)
add_dependencies(chat_room sylar)
force_redefine_file_macro_for_sources(chat_room)
target_link_libraries(chat_room ${LIB_LIB})



SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#add_executable(log ${sylar/log.h})
#add_executable(singleton sylar/singleton.h)
